# 买家账号：mxvbmb6127@sandbox.com
# 登录和支付密码：111111


import logging
from alipay.aop.api.AlipayClientConfig import AlipayClientConfig
from alipay.aop.api.DefaultAlipayClient import DefaultAlipayClient
from alipay.aop.api.domain.AlipayTradePagePayModel import AlipayTradePagePayModel
from alipay.aop.api.request.AlipayTradePagePayRequest import AlipayTradePagePayRequest

logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s %(levelname)s %(message)s',
    filemode='a',)
logger = logging.getLogger('')


alipay_public_key_string = """-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnThrwIvI/vmZTMz0iTTCPuFBNLjKNrtyF0C1/ixnUZOBoNFC+ltFaTOwdnEx7QbwmClU6ick6yuS8HFw5HJXZ/9SvgEQhO+q1CCLq6Rfin39McWcDnKEJVfmzEjK/dojysLluXeE6JtkTz1gaqMMb31YUlK9guWOldKTZz5NdU7oDlvgvls5Ah50L89uyTG70nTLciELIXiqC3bJvYHZvc5PqUzDJabBONtyCFbU4PycYIWoDbDXChUWcPA5AaAXf9xJtlbm2Hzp5qb4/XLHBwkY/U9YQh5Czkw56QsFgnS/9GhrGkKHE6igefhSDQ9lak2siRJvpyFUiPxndykV1QIDAQAB
-----END PUBLIC KEY-----"""
app_private_key_string = """-----BEGIN RSA PRIVATE KEY-----
MIIEpQIBAAKCAQEAnThrwIvI/vmZTMz0iTTCPuFBNLjKNrtyF0C1/ixnUZOBoNFC+ltFaTOwdnEx7QbwmClU6ick6yuS8HFw5HJXZ/9SvgEQhO+q1CCLq6Rfin39McWcDnKEJVfmzEjK/dojysLluXeE6JtkTz1gaqMMb31YUlK9guWOldKTZz5NdU7oDlvgvls5Ah50L89uyTG70nTLciELIXiqC3bJvYHZvc5PqUzDJabBONtyCFbU4PycYIWoDbDXChUWcPA5AaAXf9xJtlbm2Hzp5qb4/XLHBwkY/U9YQh5Czkw56QsFgnS/9GhrGkKHE6igefhSDQ9lak2siRJvpyFUiPx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jcwEFiqtW34V1dQTekmW6EpllKehDzE2YG2DoSCHQNv/bg3hk9ryC7fDGTNdGVnKb99MdqJI2OR1edn/HcwxOQCAZCB57zIpE92P//UGPu2Tf1s4JohLOsPYVqlbds5/Awg7RQHafjOcPQpEaeIw=
-----END RSA PRIVATE KEY-----"""
alipay_client_config = AlipayClientConfig()
alipay_client_config.server_url = 'https://openapi.alipaydev.com/gateway.do'
alipay_client_config.app_id = '2021000116674408'
alipay_client_config.app_private_key = app_private_key_string
alipay_client_config.alipay_public_key = alipay_public_key_string
client = DefaultAlipayClient(alipay_client_config=alipay_client_config, logger=logger)

def get_pay(out_trade_no, total_amount, return_url):
    model = AlipayTradePagePayModel()
    model.out_trade_no = out_trade_no
    model.total_amount = str(total_amount)
    model.subject = "测试"
    model.body = "支付宝测试"
    model.product_code = "FAST_INSTANT_TRADE_PAY"

    request = AlipayTradePagePayRequest(biz_model=model)

    request.notify_url = return_url + '?t=' + out_trade_no
    request.return_url = return_url + '?t=' + out_trade_no
    response = client.page_execute(request, http_method="GET")
    return response